package com.httpdemo.http_teaching_server.controller;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/uri")
public class UriVariantController {

    /* 1. Path 参数 */
    @GetMapping("/path/{id}")
    public Map<String, Object> path(@PathVariable Long id) {
        return Map.of("type", "PathVariable", "id", id);
    }

    /* 2. Query 参数 */
    @GetMapping("/query")
    public Map<String, Object> query(@RequestParam String q,
                                     @RequestParam(defaultValue = "1") int page) {
        return Map.of("type", "QueryParam", "q", q, "page", page);
    }

    /* 3. Matrix 参数（Spring 支持 ;key=value） */
    @GetMapping("/color{rgb}")
    public Map<String, Object> matrix(@MatrixVariable int R,
                                      @MatrixVariable int G,
                                      @MatrixVariable int B) {
        return Map.of("type", "MatrixVariable", "RGB", new int[]{R, G, B});
    }

    /* 4. REST 风格嵌套资源 */
    @GetMapping("/api/v1/orders/{orderId}/items/{itemId}")
    public Map<String, Object> rest(@PathVariable Long orderId,
                                    @PathVariable Long itemId) {
        return Map.of("type", "RESTful",
                "orderId", orderId,
                "itemId", itemId);
    }

    /* 5. 非 REST（传统 query） */
    @GetMapping("/getUserInfoById")
    public Map<String, Object> nonRest(@RequestParam Long id) {
        return Map.of("type", "Non-REST", "id", id);
    }
}